import { Op } from 'sequelize'
import { sequelize } from '../../../modules/BaseDao'
import Keyword from '../../../modules/decormodel/keyword'
import HistoryKeyword from '../../../modules/decormodel/historyKeyword'
// import { ShopCartRaw, ShopCartRaw_ } from '../raw'
// import { combine } from '../../commontypes'

class SearchDao {
  static shopCartDao: SearchDao = new SearchDao()

  async searchKeywords(key: string) {
    return await Keyword.findAll({
      raw: true,
      where: {
        keyword:{
          [Op.like]:`%${key}%`
        }
      },
    })
  }
  // 查询是否存在该历史关键字
  async searchHistoryKeywords(historykeyword: string){
    return await HistoryKeyword.findOne({
      raw: true,
      where: {
        historykeyword:{
          // [Op.like]:`%${historykeyword}%`
          [Op.like]:`${historykeyword}`
        }
      },
    })
  }
  // 保存历史关键字
  saveHistoryKeyWords( historykeyword: string): Promise<[any, any]> {
    const sql = `insert into historykeyword(historykeyword,clickcount) values('${historykeyword}',1)`;
    return sequelize.query(sql);
  }
  // 更新历史关键字点击次数[每次增加1)
   updateHistoryKeywordCount(historykeyword: string): Promise<[any, any]> {
    const sql = `update historykeyword set clickcount=clickcount+1 where historykeyword='${historykeyword}'`;
    return sequelize.query(sql);
   }
   // 排序获取前六条
   searchDecovery(){
    return HistoryKeyword.findAll({
      order: [['clickcount', 'desc']],
      raw:true,
      offset:0,
      limit:6,
    }
    );
   }

}

export default SearchDao.shopCartDao
